#!/usr/bin/env bash

rvm_base_except="selector"
source "$rvm_scripts_path/base"

disk_usage()
{
  \typeset directory name _pre
  name="$1"
  directory="$2"
  shift 2
  printf "%${length}s" "${name} Usage: "
  if [[ -n "$directory" && -d "$directory" && "$directory" != "/" ]]
  then
    # echo needed to clean spaces (different systems displays this differently)
    _pre="$( echo $( du -hs "$@" "${directory}/" ) )"
    echo ${_pre%% *}
  else
    echo "0B"
  fi
  return 0
}

all_disk_usage()
{
  \typeset name
  export length=30
  for name in archives repos sources logs pkg \
    rubies gemsets wrappers temporary others total
  do
    ${name}_disk_usage
  done
}

archives_disk_usage()  { disk_usage "Downloaded Archives"   "archives"; }
docs_disk_usage()      { disk_usage "Documentation"         "docs";     }
repos_disk_usage()     { disk_usage "Repositories"          "repos";    }
sources_disk_usage()   { disk_usage "Extracted Source Code" "src";      }
logs_disk_usage()      { disk_usage "Log Files"             "log";      }
pkg_disk_usage()       { disk_usage "Packages"              "usr";      }
rubies_disk_usage()    { disk_usage "Rubies"                "rubies";   }
gemsets_disk_usage()   { disk_usage "Gemsets"               "gems";     }
wrappers_disk_usage()  { disk_usage "Wrappers"              "wrappers"; }
temporary_disk_usage() { disk_usage "Temporary Files"       "tmp";      }
total_disk_usage()     { disk_usage "Total Disk"            ".";        }
others_disk_usage()
{
  \typeset flag filter
  \typeset -a flags

  if du --exclude=* . 2>/dev/null 1>/dev/null
  then flag="--exclude="
  else flag="-I "
  fi

  for filter in archives docs repos src log usr rubies gems wrappers tmp
  do flags+=( ${flag}${filter} )
  done

  disk_usage "Other Files" "." "${flags[@]}"
}

case "${1:-help}" in
  all|total|archives|docs|repos|sources|logs|pkg|rubies|gemsets|wrappers|tmp|others)
    (
      export length=""
      __rvm_cd $rvm_path
      $1_disk_usage
    )
    ;;
  help|*)
    rvm_help disk-usage
    ;;
esac
